package com.tencent.qcloud.tuikit.tuicontact.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.cqyc.network.Response;
import com.cqyc.network.getid.GetMyInformationBox;
import com.cqyc.network.http.Api;
import com.cqyc.network.http.HttpUtil;
import com.cqyc.network.http.NConfig;
import com.cqyc.network.model.GoodsInfo;
import com.cqyc.network.model.ShopInfo;
import com.cqyc.network.utils.Utils;
import com.google.gson.Gson;
import com.tencent.qcloud.tuicore.TUIConstants;
import com.tencent.qcloud.tuicore.TUICore;
import com.tencent.qcloud.tuicore.component.activities.BaseLightActivity;
import com.tencent.qcloud.tuicore.component.imageEngine.impl.GlideEngine;
import com.tencent.qcloud.tuicore.component.interfaces.IUIKitCallback;
import com.tencent.qcloud.tuicore.util.ToastUtil;
import com.tencent.qcloud.tuikit.tuicontact.R;
import com.tencent.qcloud.tuikit.tuicontact.bean.ContactItemBean;
import com.tencent.qcloud.tuikit.tuicontact.presenter.SettingProfilePresenter;
import com.tencent.qcloud.tuikit.tuicontact.ui.Dialog.TipsDialog;
import com.tencent.qcloud.tuikit.tuicontact.util.TUIContactLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;

/**
 * 买家查看用户信息
 */
public class ChatSettingActivity extends BaseLightActivity implements View.OnClickListener {
    private static final String TAG = ChatSettingActivity.class.getSimpleName();
    private SettingProfilePresenter presenter;
    private TipsDialog tipsDialog;
    private String chatId;

    private TextView name;
    private ImageView imageView;
    private TextView renQi;
    private TextView pingJia;

    private ShopInfo shopInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        TUIContactLog.i(TAG, "onCreate " + this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_setting);
        getChatInfo(getIntent());
        presenter = new SettingProfilePresenter();
        initView();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        getChatInfo(intent);
        initView();
    }

    private void getChatInfo(Intent intent) {
        chatId = intent.getStringExtra(TUIConstants.TUIChat.CHAT_ID);
    }

    private void initView() {
        String str = getIntent().getStringExtra("shopInfo");
        shopInfo = new Gson().fromJson(str, ShopInfo.class);

        findViewById(R.id.ic_back).setOnClickListener(this);
        findViewById(R.id.clear_msg).setOnClickListener(this);

        name = findViewById(R.id.name_tv);
        imageView = findViewById(R.id.image);
        renQi = findViewById(R.id.tx_r_q);
        pingJia = findViewById(R.id.tx_p_j);
        findViewById(R.id.go_to_shop).setOnClickListener(this);

        getUser();

        Switch switchTopping = findViewById(R.id.switch_topping);
        switchTopping.setChecked(presenter.isTopConversation(chatId));
        switchTopping.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                presenter.setConversationTop(chatId, b);
            }
        });

        updateMessageOptionView();
    }

    private void getUser() {
        renQi.setText(shopInfo.getPopularity());
        pingJia.setText(shopInfo.getMchEvaluation());

        presenter.getUsersInfo(chatId, new IUIKitCallback<List<ContactItemBean>>() {
            @Override
            public void onSuccess(List<ContactItemBean> data) {
                if (data == null || data.size() != 1) {
                    return;
                }
                ContactItemBean user = data.get(0);
                name.setText(user.getNickName());
                GlideEngine.loadImage(imageView, user.getAvatarUrl());
//                bean.setNickName(user.getNickName());
//                bean.setId(user.getId());
//                bean.setAvatarUrl(user.getAvatarUrl());
//                bean.setSignature(user.getSignature());
            }

            @Override
            public void onError(String module, int errCode, String errMsg) {
                ToastUtil.toastShortMessage("getUsersInfo error , code = " + errCode + ", desc = " + errMsg);
            }
        });
    }


    private void updateMessageOptionView() {
        Switch switchDisturb = findViewById(R.id.switch_disturb);

        final ArrayList<String> userIdList = new ArrayList<>();
        userIdList.add(chatId);

        presenter.getC2CReceiveMessageOpt(userIdList, new IUIKitCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean data) {
                switchDisturb.setChecked(data);
            }

            @Override
            public void onError(String module, int errCode, String errMsg) {
                switchDisturb.setChecked(false);
            }
        });

        //设置免打扰
        switchDisturb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                presenter.setC2CReceiveMessageOpt(userIdList, b);
            }
        });

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ic_back) {
            finish();
        } else if (id == R.id.clear_msg) {
            //清空历史消息记录
            if (tipsDialog == null) {
                tipsDialog = new TipsDialog(this);
            }
            tipsDialog.setTitle("提示");
            tipsDialog.setContent("确定删除所有历史消息么吗");
            tipsDialog.setData(chatId);
            tipsDialog.setSureClick(new TipsDialog.OnSureClick() {
                @Override
                public void onSureClick(Object data) {
                    Map<String, Object> hashMap = new HashMap<>();
                    hashMap.put(TUIConstants.TUIContact.FRIEND_ID, (String) data);
                    TUICore.notifyEvent(TUIConstants.TUIContact.EVENT_USER,
                            TUIConstants.TUIContact.EVENT_SUB_KEY_CLEAR_MESSAGE, hashMap);
                }
            });
            tipsDialog.show();
        } else if (id == R.id.go_to_shop) {
            Bundle bundle = new Bundle();
            Map<String, String> map = new HashMap<>();
            map.put("token", GetMyInformationBox.INSTANCE.getToken());
            map.put("app_id", GetMyInformationBox.INSTANCE.getAppId());
            map.put("android_version_code", GetMyInformationBox.INSTANCE.getVersion());
            map.put("platform", "2");

            String path = Utils.Companion.buildUrl(shopInfo.getStoreDetailUrl(), map);
            bundle.putString(NConfig.Companion.getWebViewUrl(), path);
            bundle.putBoolean(NConfig.Companion.getWebViewTitleVisible(), false);
            TUICore.startActivity(NConfig.Companion.getWebName(), bundle);
        }
    }
}
